/// <reference path="../../../../../phaser.d.ts" />

import {
  Groups
} from "./groups"

export class EnemyPlanes {
  // 敌机 类
  constructor(scene) {
    this.scene = scene
    this.init()
  }
  enemyDelay = 1500 // 敌机组总进攻速度 越小越快
  enemyTimer = null // 敌机定时器
  smallPlanes = null // 小型敌机组
  midPlanes = null // 中型敌机组
  bigPlanes = null // 大型敌机组

  // 小型敌机配置
  smallPlane = {
    speed: 2.5, // 速度
    hp: 1, // 生命值
    score: 1, //得分
    percent: 0.6, // 初始概率 1=100%
    rate: 0.6, // 当前概率
    maxRate: 0.95, // 最高难度时达到的 最大概率
    increaseRate: 0.05 // 递增的 概率
  }
  // 中型敌机配置
  midPlane = {
    speed: 2,
    hp: 4,
    score: 2,
    percent: 0.3,
    rate: 0.3,
    maxRate: 0.6,
    increaseRate: 0.04
  }
  // 大型敌机配置
  bigPlane = {
    speed: 1,
    hp: 8,
    score: 3,
    percent: 0,
    rate: 0,
    maxRate: 0.15,
    increaseRate: 0.015
  }

  init() {
    this._initAnims()

    this.smallPlanes = new Groups(this.scene)
    this.midPlanes = new Groups(this.scene)
    this.bigPlanes = new Groups(this.scene)
  }
  // 暂停
  pause() {
    this.scene.time.removeEvent(this.enemyTimer)
    this.smallPlanes.children.iterate((child) => {
      child.setVelocity(0, 0)
    })
    this.midPlanes.children.iterate((child) => {
      child.setVelocity(0, 0)
    })
    this.bigPlanes.children.iterate((child) => {
      child.setVelocity(0, 0)
    })
  }
  // 重置
  reset() {
    this.smallPlanes.allkillAndHide()
    this.midPlanes.allkillAndHide()
    this.bigPlanes.allkillAndHide()
    this._productionLineRun()

    this.smallPlane.rate = this.smallPlane.percent
    this.midPlane.rate = this.midPlane.percent
    this.bigPlane.rate = this.bigPlane.percent
  }
  // 刷新流水线计时 与 出货概率
  refresh() {
    this.enemyTimer.delay = this.enemyDelay / this.scene.speed

    this.smallPlane.rate += this.smallPlane.rate < this.smallPlane.maxRate ? this.smallPlane.increaseRate : 0
    this.midPlane.rate += this.midPlane.rate < this.midPlane.maxRate ? this.smallPlane.increaseRate : 0
    this.bigPlane.rate += this.bigPlane.rate < this.bigPlane.maxRate ? this.bigPlane.increaseRate : 0
  }
  // 清除敌机
  killEnemy(enemyPlane) {
    if (enemyPlane.hp <= 0) {
      enemyPlane.anims.play(enemyPlane.name + 'Boom')
      this.scene.extraScore(enemyPlane.score) // 加分
      enemyPlane.setVelocity(0, 0)
      // 动画结束时 回收敌机
      enemyPlane.once('animationcomplete', () => {
        let enemyPlanes = null
        if (enemyPlane.name == 'smallPlane') enemyPlanes = this.smallPlanes
        else if (enemyPlane.name == 'midPlane') enemyPlanes = this.midPlanes
        else if (enemyPlane.name == 'bigPlane') enemyPlanes = this.bigPlanes
        enemyPlanes.killAndHide(enemyPlane)
      }, this);
    } else if (enemyPlane.name == 'midPlane' || enemyPlane.name == 'bigPlane') {
      if (enemyPlane.hp <= enemyPlane.fullHP / 2) {
        enemyPlane.anims.play(enemyPlane.name + 'Hit')
      }
    }
  }
  // 捡到炸弹道具 清除全屏敌机
  killAllEnemys(player, supply) {
    // 消耗道具
    player = player.gameObject
    supply = supply.gameObject
    if (!player.active || !supply.active) return
    if (this.scene.audio) this.scene.sounds.getBuff.play()
    this.scene.propSupply.bombs.killAndHide(supply)
    this.scene.show('清屏炸弹', player.x, player.y)
    this.smallPlanes.children.iterate((child) => {
      if (child.hp > 0) {
        child.hp = 0
        this.killEnemy(child)
      }
    })
    this.midPlanes.children.iterate((child) => {
      if (child.hp > 0) {
        child.hp = 0
        this.killEnemy(child)
      }
    })
    this.bigPlanes.children.iterate((child) => {
      if (child.hp > 0) {
        child.hp = 0
        this.killEnemy(child)
      }
    })
  }
  // 敌机流水线 启动
  _productionLineRun() {
    this.enemyTimer = this.scene.time.addEvent({
      delay: this.enemyDelay / this.scene.speed,
      loop: true,
      callback: () => {
        // 抽卡模拟器
        if (Math.random() <= this.smallPlane.rate) this._createSmallPlane()
        if (Math.random() <= this.midPlane.rate) this._createMidPlane()
        if (Math.random() <= this.bigPlane.rate) this._createBigPlane()
      }
    })
  }
  // 生成 小型敌机
  _createSmallPlane() {
    let x = 17 + Math.ceil(Math.random() * (this.scene.gameWidth - 34))
    let y = -12
    let plane = this.smallPlanes.get(x, y, 'smallPlane', null, {
      shape: this.scene.cache.json.get('shapes').smallPlane
    })
    plane.name = 'smallPlane'
    plane.hp = this.smallPlane.hp
    plane.fullHP = this.smallPlane.hp
    plane.score = this.smallPlane.score
    plane.setVelocity(0, this.smallPlane.speed * this.scene.speed)
    plane.anims.play('smallPlaneRun')
  }
  // 生成 中型敌机
  _createMidPlane() {
    let x = 32 + Math.ceil(Math.random() * (this.scene.gameWidth - 64))
    let y = -32
    let plane = this.midPlanes.get(x, y, 'midPlane', null, {
      shape: this.scene.cache.json.get('shapes').midPlane
    })
    plane.name = 'midPlane'
    plane.hp = this.midPlane.hp
    plane.fullHP = this.midPlane.hp
    plane.score = this.midPlane.score
    plane.setVelocity(0, this.midPlane.speed * this.scene.speed)
    plane.anims.play('midPlaneRun')
  }
  // 生成 大型敌机
  _createBigPlane() {
    let x = 55 + Math.ceil(Math.random() * (this.scene.gameWidth - 110))
    let y = -170
    let plane = this.bigPlanes.get(x, y, 'bigPlane', null, {
      shape: this.scene.cache.json.get('shapes').bigPlane
    })
    plane.name = 'bigPlane'
    plane.hp = this.bigPlane.hp
    plane.fullHP = this.bigPlane.hp
    plane.score = this.bigPlane.score
    plane.setVelocity(0, this.bigPlane.speed * this.scene.speed)
    plane.anims.play('bigPlaneRun')
  }

  update() {
    // 敌机更新回收
    let gameHeight = this.scene.gameHeight
    this.smallPlanes.children.iterate((child) => {
      if (child.y - child.height / 2 > gameHeight) {
        this.smallPlanes.killAndHide(child)
      }
    })
    this.midPlanes.children.iterate((child) => {
      if (child.y - child.height / 2 > gameHeight) {
        this.midPlanes.killAndHide(child)
      }
    })
    this.bigPlanes.children.iterate((child) => {
      if (child.y - child.height / 2 > gameHeight) {
        this.bigPlanes.killAndHide(child)
      }
    })
  }
  // 初始化动画
  _initAnims() {
    // 小敌机正常
    this.scene.anims.create({
      key: 'smallPlaneRun',
      frames: [{
        key: 'smallPlane',
        frame: 0
      }],
      frameRate: 8
    })
    // 小敌机摧毁
    this.scene.anims.create({
      key: 'smallPlaneBoom',
      frames: this.scene.anims.generateFrameNumbers('smallPlane', {
        start: 1,
        end: 4
      }),
      frameRate: 32
    })

    // 中敌机正常
    this.scene.anims.create({
      key: 'midPlaneRun',
      frames: [{
        key: 'midPlane',
        frame: 0
      }],
      frameRate: 8,
      repeat: -1
    })
    // 中敌机受弹 hp/2
    this.scene.anims.create({
      key: 'midPlaneHit',
      frames: [{
        key: 'midPlane',
        frame: 1
      }],
      frameRate: 8,
    })
    // 中敌机摧毁
    this.scene.anims.create({
      key: 'midPlaneBoom',
      frames: this.scene.anims.generateFrameNumbers('midPlane', {
        start: 2,
        end: 5
      }),
      frameRate: 24
    })

    // 大敌机正常
    this.scene.anims.create({
      key: 'bigPlaneRun',
      frames: [{
        key: 'bigPlane',
        frame: 0
      }],
      frameRate: 8,
    })
    // 大敌机受弹 hp/2
    this.scene.anims.create({
      key: 'bigPlaneHit',
      frames: [{
        key: 'bigPlane',
        frame: 1
      }],
      frameRate: 8,
    })
    // 大敌机摧毁
    this.scene.anims.create({
      key: 'bigPlaneBoom',
      frames: this.scene.anims.generateFrameNumbers('bigPlane', {
        start: 2,
        end: 7
      }),
      frameRate: 24
    })
  }
}
